package org.limit.bookServe.service.impl;

import org.limit.bookServe.bean.User;
import org.limit.bookServe.service.UserService;
import org.limit.bookServe.utils.BaseClass;

import java.util.Map;
import java.util.Objects;

import static org.limit.bookServe.bean.User.UserAccStatus.DISABLE;
import static org.limit.bookServe.bean.User.UserAccStatus.NORMAL;
import static org.limit.bookServe.utils.SystemConstants.SHOW_USERS_INFO_LIST;
import static org.limit.bookServe.utils.SystemData.saveUserData;

/**
 * @author 1.
 * @version 1.0
 * @description:  用户服务实现类
 * @date 2023/11/1 20:07
 */
public class UserServiceImpl extends BaseClass implements UserService {

    private final Map<String, User> users;

    public UserServiceImpl(Map<String, User> users) {
        this.users = users;
    }

    @Override
    public void register() {
        //注册新用户
        boolean res = true;
        while (res) {
            print("请输入您的用户名: ");
            String username = input.nextLine();
            print("请输入您的密码: ");
            String password = input.nextLine();

            if(checkedForUserNameAndPassword(username, password) &&
                    findByUserNameAndPassword(username, password) == null) {
                User user = new User(username, password);
                //存入用户缓存桶
                users.put(user.getUserName(), user);
                //数据持久化
                saveUserData();
                //将用户信息放入当前线程
                setCurrentUser(user);
                res = false;
                System.out.println("注册成功！");
//                return;
            }
        }

    }

    public User findByUserNameAndPassword(String username, String password) {
        if (users.containsKey(username)) {
            println("用户已存在！");
            User user = users.get(username);
            if (Objects.equals(user.getPassWord(), password)) {
                return user;
            }
            println("密码错误！");
        }
        return null;
    }

    public boolean checkedForUserNameAndPassword(String username, String password) {
        if ((Objects.equals(username, "") || Objects.equals(password, ""))) {
            System.out.println("用户名或密码不能为空！");
            return false;
        }
        return true;
    }

    @Override
    public void login() {
        print("请输入您的用户名: ");
        String username = input.nextLine();
        print("请输入您的密码: ");
        String password = input.nextLine();
        User user;
        if (checkedForUserNameAndPassword(username, password) &&
                (user = findByUserNameAndPassword(username,password)) != null) {
            //存入线程
            setCurrentUser(user);
            //返回
            println("登陆成功! 您的身份是 【 " + user.getIdentity().getDesc() + " 】");
        } else {
            println("登录失败！");
        }
    }

    @Override
    public void findUserByUserName(String username) {
        if (users.containsKey(username)) println(String.valueOf(users.get(username)));
        else println("未查找到用户信息！");
    }

    @Override
    public void showAllUserInfo() {
        println(resourceReader(SHOW_USERS_INFO_LIST));
        if (users != null && !users.isEmpty()) {
            for (User u : users.values()) {
                println(u.toString());
            }
        }
        //换行
        println();
    }

    @Override
    public void bannedUserAcc(String uid) {
        boolean flag = false;
        for (User u : users.values()) {
            if (Objects.equals(u.getUid(), uid)) {
                //修改账号状态
                u.setStatus(DISABLE);
                //修改后数据持久化
                saveUserData();
                println("修改成功！");
                flag = true;
            }
        }
        if (!flag) println("修改失败，未查找到指定id的用户！");
    }

    @Override
    public void changedUserStatus(String uid) {
        boolean flag = false;
        for (User u : users.values()) {
            if (Objects.equals(u.getUid(), uid)) {
                println("当前用户帐号状态为: 【 " + u.getStatus().getDesc() + " 】");
                User.UserAccStatus currentStatus = u.getStatus() == NORMAL ? DISABLE : NORMAL;
                print("你希望将该用户账号修改为:【 " + currentStatus.getDesc() + " 】 ? [yes/no]：");
                String sel = input.nextLine();
                if (!Objects.equals(sel, "") && sel.equals("yes")) {
                    u.setStatus(currentStatus);
                    //修改数据持久化
                    saveUserData();
                    flag = true;
                    println("修改成功！");
                } else if (!Objects.equals(sel, "")) {
                    println("输入有误！");
                    println();
                    return;
                }
            }
        }
        if (!flag) println("修改失败，未查找到指定id的用户！");
        //换行
        println();
    }

}
